/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elicitor.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import org.encog.Encog;
import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.propagation.back.Backpropagation;
import org.encog.persist.EncogDirectoryPersistence;

/**
 *
 * @author dileepa
 */
public class NeuralNetworkFiveInput {

    public static BasicNetwork network;
    public static double inputValues[][] = new double[120][5];
    public static double realTimeinputValues[][] = new double[1][5];
    public static double outPutValues[][] = new double[120][6];
    public static double realTimeoutPutValues[][] = new double[1][6];
    String realTimeFileLocation;
    Double emotionArray[] = new Double[6];

    public static void putOutInPut() {

        Scanner scanner = null;

        File file = new File("selection.csv");

        try {

            scanner = new Scanner(file);
            int lineNum = 0;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String result[] = line.split(",");

                if (result.length == 11) {

                    for (int i = 0; i < result.length; i++) {

                        if (i <= 4) {
                            inputValues[lineNum][i] = Double.parseDouble(result[i]);
                        } else {
                            outPutValues[lineNum][i - 5] = Double.parseDouble(result[i]);
                        }


                    }
                    lineNum++;
                }

            }



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }

    }

    public NeuralNetworkFiveInput(String string) {
        this.realTimeFileLocation = string;
    }

    public Double[] getEmotions() {

        putOutInPut();


        network = new BasicNetwork();
        network.addLayer(new BasicLayer(null, true, 5));
        network.addLayer(new BasicLayer(new ActivationSigmoid(), true, 20));
        network.addLayer(new BasicLayer(new ActivationSigmoid(), false, 6));
        network.getStructure().finalizeStructure();
        network.reset();

        // create training data
        MLDataSet trainingSet = new BasicMLDataSet(inputValues, outPutValues);

        // train the neural network

        final Backpropagation train = new Backpropagation(network, trainingSet, 0.2, 0.3);

        int epoch = 1;

        do {
            train.iteration();
            System.out.println("Epoch #" + epoch + " Error:" + train.getError());
            epoch++;
        } while (train.getError() > 0.125);

        // test the neural network
//        System.out.println("Neural Network Results:");
//        for (MLDataPair pair : trainingSet) {
//            final MLData output = network.compute(pair.getInput());
//            System.out.println(pair.getInput().getData(0) + "," + pair.getInput().getData(1)
//                    + ", actual 0=" + output.getData(0) + ",ideal 0=" + pair.getIdeal().getData(0) + ", actual 1=" + output.getData(1) + ",ideal 1=" + pair.getIdeal().getData(1) + ", actual 2=" + output.getData(2) + ",ideal 2=" + pair.getIdeal().getData(2) + ", actual 3=" + output.getData(3) + ",ideal 3=" + pair.getIdeal().getData(3) + ", actual 4=" + output.getData(4) + ",ideal 4=" + pair.getIdeal().getData(4) + ", actual 5=" + output.getData(5) + ",ideal 5=" + pair.getIdeal().getData(5));
//        }

        Scanner scanner = null;
        try {
            File file = new File(realTimeFileLocation);
            scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String result[] = line.split(",");
                if (result.length == 29) {
                    realTimeinputValues[0][0] = Double.parseDouble(result[24]);
                    realTimeinputValues[0][1] = Double.parseDouble(result[25]);
                    realTimeinputValues[0][2] = Double.parseDouble(result[26]);
                    realTimeinputValues[0][3] = Double.parseDouble(result[27]);
                    realTimeinputValues[0][4] = Double.parseDouble(result[28]);

                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }


        realTimeoutPutValues[0][0] = 0;
        realTimeoutPutValues[0][1] = 0;
        realTimeoutPutValues[0][2] = 0;
        realTimeoutPutValues[0][3] = 0;
        realTimeoutPutValues[0][4] = 0;
        realTimeoutPutValues[0][5] = 0;

        MLDataSet evolutiondata = new BasicMLDataSet(realTimeinputValues, realTimeoutPutValues);

        for (MLDataPair pair : evolutiondata) {
            final MLData output = network.compute(pair.getInput());
            double emotionTotal = 0.0;
            for(int i=0;i<6;i++){
                emotionArray[i]=output.getData(i);
                emotionTotal = emotionTotal+emotionArray[i];
            }
            for(int i=0;i<6;i++){
                System.out.println("cccccccccccc"+emotionArray[i]);
                emotionArray[i]=(emotionArray[i]/emotionTotal)*100;
                System.out.println("BNNNNNNNN"+emotionArray[i]);
            }
            
        }

        Encog.getInstance().shutdown();
        return emotionArray;

    }
}
